<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>权限分配</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/admin/style/admin.css" media="all">
    <link rel="stylesheet" href="/static/common/js/zTree/zTreeStyle.css" type="text/css">
    <style>
        .ztree *{font-size: 14px}
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <form class="layui-form" action="" lay-filter="component-form-element">
                        <div class="layui-row layui-col-space10 layui-form-item">
                            <div class="layui-col-lg6">
                                <div id="treeType" style="margin: 0 auto" class="ztree"></div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="component-form-element">立即提交</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="/static/layui/layui.js"></script>
<script src="/static/common/js/jquery.min.js"></script>
<script src="/static/common/js/layTool.js"></script>
<script src="/static/common/js/zTree/jquery.ztree.core-3.5.js"></script>
<script src="/static/common/js/zTree/jquery.ztree.excheck-3.5.js"></script>
<script src="/static/common/js/zTree/jquery.ztree.exedit-3.5.js"></script>
<script>

    layui.use(['tree', 'form'], function () {

        var tree = layui.tree;
        var form = layui.form;

        // 表单提交
        form.on('submit(component-form-element)', function(data){

            var zTree = $.fn.zTree.getZTreeObj("treeType");
            var nodes = zTree.getCheckedNodes(true);
            var NodeString = '';

            $.each(nodes, function (n, value) {
                if(n > 0){
                    NodeString += ',';
                }
                NodeString += value.id;
            });

            $.post("{:url('role/assignAuthority')}", {node: NodeString, role_id: {$role_info['role_id']}}, function (res) {

                if(0 == res.code) {

                    layer.msg(res.msg);
                    setTimeout(function () {

                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        window.parent.renderTable();
                    }, 200);
                } else {
                    layTool.alert(res.msg, '友情提示', 2);
                }
            }, 'json');
            return false;
        });
    });

    $(function () {

        var zNodes = {$tree|raw};

        // 设置zetree
        var setting = {
            check:{
                enable:true
            },
            data: {
                simpleData: {
                    enable: true
                }
            }
        };

        $.fn.zTree.init($("#treeType"), setting, zNodes);
        var zTree = $.fn.zTree.getZTreeObj("treeType");
        //zTree.expandAll(true);
    });

</script>
</body>
</html>